﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using RWLib;

namespace RW_GUI
{
    /// <summary>
    /// Interaction logic for AddActionWindow.xaml
    /// </summary>
    public partial class AddActionWindow : Window
    {
        #region Properties
        public bool valid = false, value;
        private List<Fluent> _fluents;
        private List<ActionModel> _actions;
        private bool lastfocusEffects = false;
        public ActionModel Model;
        #endregion
        public AddActionWindow(List<Fluent> fluents, List<ActionModel> actions)
        {
            _fluents = fluents;
            _actions = actions;
            InitializeComponent();
            txt_Name.Focus();
            LoadCombo();
        }
        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            valid = true;
            var action = CB_Actions.SelectedValue != null ? (string)((ComboBoxItem)CB_Actions.SelectedValue).Content : "";
            ActionTypeEnum en;
            Enum.TryParse(((ComboBoxItem)CB_Type.SelectedItem).Content as String, out en);
            Model = new ActionModel(
                txt_Name.Text.Trim(), txt_ReqFormula.Text, txt_EffFormula.Text,
                action, (int)Slider_Time.Value, en);
            Close();
        }
        private void btn_AddFluentRequirements_Click(object sender, RoutedEventArgs e)
        {
            var fcw = new FluentChoiceWindow(_fluents, String.IsNullOrEmpty(txt_ReqFormula.Text.Trim()));
            fcw.ShowDialog();
            txt_ReqFormula.Text += fcw.Name;
        }
        private void btn_AddFluentEffects_Click(object sender, RoutedEventArgs e)
        {
            var fcw = new FluentChoiceWindow(_fluents, String.IsNullOrEmpty(txt_EffFormula.Text.Trim()));
            fcw.ShowDialog();
            txt_EffFormula.Text += fcw.Name;
        }
        private void LoadCombo()
        {
            foreach (var v in typeof(ActionTypeEnum).GetEnumNames())
            {
                CB_Type.Items.Add(new ComboBoxItem { Content = v });
            }
            CB_Type.SelectedIndex = 0;

            foreach (var v in _actions.Select(x => x.Name))
            {
                CB_Actions.Items.Add(new ComboBoxItem { Content = v });
            }
            CB_Actions.SelectedIndex = 0;
        }
        private void SliderTimeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (sender != null && Txt_Time != null)
                Txt_Time.Content = ((Slider)sender).Value;
        }
        private void CB_Type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CB_Actions == null) return;
            ActionTypeEnum en;
            Enum.TryParse((string)((ComboBoxItem)CB_Type.SelectedValue).Content, out en);
            Grid_Actions.EnableChildren();
            Grid_Eff.EnableChildren();
            Grid_Req.EnableChildren();
            Grid_Time.EnableChildren();
            txt_Name.IsEnabled = true;
            switch (en)
            {
                case ActionTypeEnum.Causes:
                    Grid_Time.DisableChildren();
                    Grid_Actions.DisableChildren();
                    break;
                case ActionTypeEnum.Impossible:
                    txt_Name.IsEnabled = false;
                    Grid_Eff.DisableChildren();
                    Grid_Time.DisableChildren();
                    break;
                case ActionTypeEnum.ImpossibleAtTime:
                    txt_Name.IsEnabled = false;
                    Grid_Eff.DisableChildren();
                    Grid_Req.DisableChildren();
                    break;
                case ActionTypeEnum.Invokes:
                    Grid_Eff.DisableChildren();
                    Grid_Req.DisableChildren();
                    break;
                case ActionTypeEnum.Releases:
                    Grid_Time.DisableChildren();
                    Grid_Actions.DisableChildren();
                    break;
                case ActionTypeEnum.Trigger:
                    txt_Name.IsEnabled = false;
                    Grid_Eff.DisableChildren();
                    Grid_Time.DisableChildren();
                    break;
            }
        }
    }
}